//
//  TaxesHistoryViewController.m
//  Rong360
//
//  Created by tianliwei on 10/2/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "TaxesHistoryViewController.h"
#import "DBMan.h"
#import "NewHouseHisCell.h"
#import "SecondHouseHisCell.h"
@interface TaxesHistoryViewController ()
@property (nonatomic, strong) UISegmentedControl *segmentedControl;

@end

static NSString *CellIdentifier_New = @"Cell_New";
static NSString *CellIdentifier_Second = @"Cell_Second";
@implementation TaxesHistoryViewController

- (id)initWithHousehandType:(HouseHandType)houseHand{
    self = [super init];
    if (self) {
        self.handType = houseHand;
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)initSegmentControl{
    
    NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"二手房",@"新房",nil];
    
    //初始化UISegmentedControl
    
    self.segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    
    self.segmentedControl.frame = CGRectMake(10.0, 10.0, 300.0, 35.0);
    
    DBMan* dbm = [DBMan shareDB];

    if (self.handType == HouseSecondHand) {
        self.segmentedControl.selectedSegmentIndex = 0;//设置默认选择项索引
        self.secTimeArr = [dbm getAllSectionTimeInSecondTaxesHist];
        self.totalDic = [dbm getAllDicInSecondTaxesHist];
    }else if (self.handType == HouseNew){
        self.segmentedControl.selectedSegmentIndex = 1;
        self.secTimeArr = [dbm getAllSectionTimeInNewTaxesHist];
        self.totalDic = [dbm getAllDicInNewTaxesHist];
    }
    
    self.segmentedControl.tintColor = RGBCOLOR(0, 172, 238);
    
    //有基本四种样式
    self.segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;//设置样式
    
    [self.segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];  //添加委托方法
    [self.view addSubview:self.segmentedControl];
}

//具体委托方法实例

-(void)segmentAction:(UISegmentedControl *)Seg{
    NSInteger Index = Seg.selectedSegmentIndex;
    DBMan* dbm = [DBMan shareDB];
    switch (Index) {
        case 0:
            self.handType = HouseSecondHand;
            self.secTimeArr = [dbm getAllSectionTimeInSecondTaxesHist];
            self.totalDic = [dbm getAllDicInSecondTaxesHist];
            break;
        case 1:
            self.handType = HouseNew;
            self.secTimeArr = [dbm getAllSectionTimeInNewTaxesHist];
            self.totalDic = [dbm getAllDicInNewTaxesHist];
            break;
        default:
            break;
    }
    [self.tableView reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.title = @"税费计算历史";
    DBMan* dbm = [DBMan shareDB];
    self.secTimeArr = [dbm getAllSectionTimeInSecondTaxesHist];
    self.totalDic = [dbm getAllDicInSecondTaxesHist];
//    self.view.backgroundColor = RGBCOLOR(245, 245, 245);
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(IOS7_OR_LATER?0:-10, 50, UI_SCREEN_WIDTH, UI_NAVIFRAME_HEIGHT-50)style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 56;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.backgroundView = nil;
    [self.tableView registerNib:[UINib nibWithNibName:@"NewHouseHisCell" bundle:nil] forCellReuseIdentifier:CellIdentifier_New];
    [self.tableView registerNib:[UINib nibWithNibName:@"SecondHouseHisCell" bundle:nil] forCellReuseIdentifier:CellIdentifier_Second];
    [self.view addSubview:self.tableView];
    [self initSegmentControl];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 30;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    ToolOperation* tool = [ToolOperation shareTool];

    UILabel* lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(15, 29, 305, 1)];
    line.backgroundColor = RGBCOLOR(219, 219, 219);
    [lab addSubview:line];
    lab.backgroundColor = [UIColor whiteColor];
    lab.textAlignment = NSTextAlignmentLeft;
    lab.font = [UIFont fontWithName:@"Arial-BoldMt" size:18];
    lab.textColor = [UIColor colorWithRed:101.0/255.0 green:101.0/255.0 blue:101.0/255.0 alpha:1];
    
    lab.text = [NSString stringWithFormat:@"   %@",[tool getTimeIsToday:[self.secTimeArr objectAtIndex:section]]];
    lab.shadowColor = [UIColor whiteColor];
    lab.shadowOffset = CGSizeMake(0, 1.0f);
    return lab;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSLog(@"--------->%d",[self.secTimeArr count]);
    return [self.secTimeArr count];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSString* timeStr = [self.secTimeArr objectAtIndex:section];
    NSMutableArray* array = [self.totalDic objectForKey:timeStr];
    
    return [array count];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString* title = [self.secTimeArr objectAtIndex:indexPath.section];
    NSMutableArray* array = [self.totalDic objectForKey:title];
    if (self.handType == HouseNew) {
        NewHouseHisCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier_New];
        cell.backgroundView = [[UIView alloc]init];
        cell.backgroundColor = [UIColor whiteColor];
        TaxesNewHisUnit * hut = [array objectAtIndex:indexPath.row];
        cell.priceLabel.text = [NSString stringWithFormat:@"房价%@万，面积%@平米",hut.price,hut.square];
        cell.payLabel.text = hut.totaltaxes;
        return cell;
    }else if (self.handType == HouseSecondHand){
        SecondHouseHisCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier_Second];
        cell.backgroundView = [[UIView alloc]init];
        cell.backgroundColor = [UIColor whiteColor];
        TaxesSecondHisUnit *hut = [array objectAtIndex:indexPath.row];
        cell.priceLabel.text = [NSString stringWithFormat:@"房价%@万，面积%@平米",hut.price,hut.square];
        cell.overFive.text = [NSString stringWithFormat:@"%@满五，%@唯一",(hut.overfive?@"":@"不"),(hut.onlyone?@"":@"不")];
        cell.payLabel.text = hut.totaltaxes;
        return cell;
    }
    return nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
